package com.byf.product.service.impl;

import com.byf.product.config.ItemThreadPoolProperties;
import com.byf.product.config.ProductThreadPoolConfig;
import com.byf.product.domain.PmsSkuImages;
import com.byf.product.domain.PmsSkuInfo;
import com.byf.product.domain.PmsSpuInfoDesc;
import com.byf.product.mapper.ItemInfoMapper;
import com.byf.product.mapper.PmsSkuImagesMapper;
import com.byf.product.mapper.PmsSkuInfoMapper;
import com.byf.product.mapper.PmsSpuInfoDescMapper;
import com.byf.product.service.*;
import com.byf.product.vo.AttrGroupVo;
import com.byf.product.vo.ItemInfoVo;
import com.byf.product.vo.SaleAttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

@Service
public class ItemInfoServiceImpl implements ItemInfoService {
    @Autowired
    private ItemInfoMapper itemInfoMapper;

    @Autowired
    private PmsSkuInfoMapper pmsSkuInfoMapper;

    @Autowired
    private PmsSkuImagesMapper pmsSkuImagesMapper;

    @Autowired
    private IPmsSkuSaleAttrValueService iPmsSkuSaleAttrValueService;

    @Autowired
    private PmsSpuInfoDescMapper pmsSpuInfoDescMapper;

    @Autowired
    private IPmsAttrGroupService iPmsAttrGroupService;

    @Autowired
    private ThreadPoolExecutor itemThreadPoolExcuter;

    /**
     * 获取商品规格与包装
     * @param skuId
     * @return
     */
    @Override
    public ItemInfoVo getItemInfo(Long skuId) throws ExecutionException, InterruptedException {
        //         * 1.查sku详情   0.5s
        ItemInfoVo itemInfoVo = new ItemInfoVo();
        CompletableFuture<PmsSkuInfo> pmsSkuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            PmsSkuInfo pmsSkuInfo = pmsSkuInfoMapper.selectPmsSkuInfoBySkuId(skuId);
            itemInfoVo.setSkuInfo(pmsSkuInfo);
            return pmsSkuInfo;
        }, itemThreadPoolExcuter);

//     *
//             * 2.查sku图片信息 1s
        CompletableFuture<Void> pmsSkuImagesCompletableFuture = CompletableFuture.runAsync(() -> {
            PmsSkuImages pmsSkuImages = new PmsSkuImages();
            pmsSkuImages.setSkuId(skuId);
            List<PmsSkuImages> pmsSkuImagesList = pmsSkuImagesMapper.selectPmsSkuImagesList(pmsSkuImages);
            itemInfoVo.setSkuImagesList(pmsSkuImagesList);
        }, itemThreadPoolExcuter);
//     *
//             * 3.查spu相关信息销售属性 1s
        CompletableFuture<Void> saleAttrVosCompletableFuture = CompletableFuture.runAsync(() -> {
            List<SaleAttrVo> saleAttrVos = iPmsSkuSaleAttrValueService.selectSkuSaleAttrsValueBySkuId(skuId);
            itemInfoVo.setSaleAttrs(saleAttrVos);
        }, itemThreadPoolExcuter);
//     *
//             * 4.查spu商品介绍 1s
        CompletableFuture<Void> pmsSpuInfoDescCompletableFuture = pmsSkuInfoCompletableFuture.thenAcceptAsync(pmsSkuInfo -> {
            PmsSpuInfoDesc pmsSpuInfoDesc = pmsSpuInfoDescMapper.selectPmsSpuInfoDescBySpuId(pmsSkuInfo.getSpuId());
            itemInfoVo.setSpuInfoDesc(pmsSpuInfoDesc);
        }, itemThreadPoolExcuter);

//     *
//             * 5.查规格和包装 05s
        CompletableFuture<Void> attrGroupVosDescCompletableFuture = pmsSkuInfoCompletableFuture.thenAcceptAsync(pmsSkuInfo -> {
            List<AttrGroupVo> attrGroupVos = iPmsAttrGroupService.selectAttrsGroupByCatagoryId(pmsSkuInfo.getCatalogId());
            itemInfoVo.setAttrGroupVos(attrGroupVos);
        }, itemThreadPoolExcuter);

        CompletableFuture.allOf(pmsSkuInfoCompletableFuture,
                pmsSkuImagesCompletableFuture,saleAttrVosCompletableFuture,
                pmsSpuInfoDescCompletableFuture,
                attrGroupVosDescCompletableFuture)
                .get();

        return itemInfoVo;
    }
}
